package rbac

import (
	"github.com/gin-gonic/gin"
	"store/libs/helper"
	"store/models"
	"store/models/base"
)

const (
	TABLE_ADMIN = "admin"
)

type Admin struct {
	Id           uint64 `json:"id" xml:"id"`
	IsOn         uint8  `json:"is_on" xml:"is_on"`
	IsSuperAdmin uint8  `json:"is_super_admin" xml:"is_super_admin"`
	LastLoginIp  uint64 `json:"last_login_ip" xml:"last_login_ip"`
	UpdateTime   uint64 `json:"update_time" xml:"update_time"`
	UserName     string `json:"user_name" xml:"user_name"`
	TrueName     string `json:"true_name" xml:"true_name"`
	Password     string `json:"password" xml:"password"`
	AddTime      string `json:"add_time" xml:"add_time"`
	Version      uint64 `json:"version" xml:"version"`
}

/**
 * @email jhq0113@163.com
 * generated by gii
 */
/**
 * @email jhq0113@163.com
 * generated by gii
 */
func AdminMap2Model(data map[string]string) Admin {
	return Admin{
		UserName:     data["user_name"],
		TrueName:     data["true_name"],
		Password:     data["password"],
		AddTime:      data["add_time"],
		Version:      uint64(helper.ToInt(data["version"])),
		Id:           uint64(helper.ToInt(data["id"])),
		IsOn:         uint8(helper.ToInt(data["is_on"])),
		IsSuperAdmin: uint8(helper.ToInt(data["is_super_admin"])),
		LastLoginIp:  uint64(helper.ToInt(data["last_login_ip"])),
		UpdateTime:   uint64(helper.ToInt(data["update_time"])),
	}
}

func GetUserByUserName(userName string) *Admin {
	user := new(Admin)
	models.Orm.Table(TABLE_ADMIN).Where("user_name = ?", userName).First(user)
	return user
}

/**restful新增数据
 * @email jhq0113@163.com
 * generated by gii
 */
func AdminCreate(ctx *gin.Context, info map[string]interface{}) uint64 {
	id := base.NewCommonModel(ctx).Create(TABLE_ADMIN, info)
	return uint64(id)
}

/**restful删除，数据库表中必须有id字段
 * @email jhq0113@163.com
 * generated by gii
 */
func AdminDelete(ctx *gin.Context, id uint64) int64 {
	return base.NewCommonModel(ctx).Delete(TABLE_ADMIN, helper.ToString(id))
}

/**restful更新(带乐观锁)，数据库表中必须有id,version字段
uint64uint64uint64 * @email jhq0113@163.com
 * generated by gii
*/
func AdminUpdate(ctx *gin.Context, id uint64, version uint64, set map[string]interface{}) int64 {
	return base.NewCommonModel(ctx).UpdateWithLock(TABLE_ADMIN, helper.ToString(id), version, set)
}

/**restful禁用启用，数据库表中必须有is_on字段
 * @email jhq0113@163.com
 * generated by gii
 */
func AdminUpOrDown(ctx *gin.Context, id uint64, value uint8) int64 {
	return base.NewCommonModel(ctx).UpOrDown(TABLE_ADMIN, helper.ToString(id), value)
}

/**restful详情，数据库表中必须有id字段
 * @email jhq0113@163.com
 * generated by gii
 */
func AdminInfo(ctx *gin.Context, where map[string]interface{}) Admin {
	info := base.NewCommonModel(ctx).Info(TABLE_ADMIN, where)
	if info != nil {
		return AdminMap2Model(info)
	}
	return Admin{}
}

/**restful列表分页检索
 * @email jhq0113@163.com
 * generated by gii
 */
func AdminIndex(ctx *gin.Context, page int64, pageSize int64, where map[string]interface{}, orders []string) map[string]interface{} {
	result := base.NewCommonModel(ctx).Index(TABLE_ADMIN, page, pageSize, where, orders)

	if len(result["list"].([]map[string]string)) > 0 {
		list := []Admin{}
		for _, data := range result["list"].([]map[string]string) {
			data["password"] = ""
			list = append(list, AdminMap2Model(data))
		}
		result["list"] = list
	}

	return result
}
